package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;

import Controller.AddNewCustomerController;
import Entity.Customer;
import DB.CustomerQuery;
import Communication.Client;
import Communication.MessageHandle;
import View.*;

/** This class controls and manages the process that allows to add new customer to the system */
public class AddNewCustomerController {
	/**  Required attributes */
	CustomerQuery customerModel;
	static AddNewCustomerView customerView;
	Client client;
	static Customer Cst;
	static ArrayList<Customer> customersList=new ArrayList<Customer>();
    final Calendar c = Calendar.getInstance();
		
	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 * @param workerMainMenuController
	 */
	public AddNewCustomerController(Client client, WorkerMainMenuController workerMainMenuController) {

		this.client = client;
		customerView =  new AddNewCustomerView();
		customerView.showWindow();
		customerView.addNewCustomerListener(new CustomerInfoListener(workerMainMenuController));
		getCites("addNewCustomer");
		
	}
	
	/**
	 * function that gets all the cities in the system
	 * @param message
	 */
	private void getCites(String message) {
		MessageHandle msg = new MessageHandle(null, MessageHandle.GET_CITIES, false);
		msg.setMsg(message);
		try {
			client.sendToServer(msg);
		} catch (IOException e) {
			System.out.print("AddNewCustController->GetCities");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**  Inner Listener class */ 
	class CustomerInfoListener implements ActionListener
	{
		WorkerMainMenuController workerMainController;
		
		
		public CustomerInfoListener(WorkerMainMenuController workerMainController){

			this.workerMainController = workerMainController;
			customerModel = new CustomerQuery();
		
			workerMainController.hideWindow();
		}
		
		@Override
		/**
		 * handle actions
		 */
		public void actionPerformed(ActionEvent event) {
			
			if (event.getSource() == customerView.getAddBtn())
			{
				if (customerModel.isEmpty(customerView.getlastName()) || customerModel.isEmpty(customerView.getFirstName()) ||
						customerModel.isEmpty(customerView.getID())  ||
						customerModel.isEmpty(customerView.getPhone()) || customerModel.isEmpty(customerView.getAge())  
					|| customerModel.isEmpty(customerView.getCity()))
					SystemController.displayErrorMessage("One or more fields are Empty");
				
				else 				
							 
					
				{  customerView.showWindow();
				
			
					 Cst=new Customer();
					Cst.setCfirstName(customerView.getFirstName());
					Cst.setClastName(customerView.getlastName());
					Cst.setCustomerID(customerView.getID());
					Cst.setPhoneNumber(customerView.getPhone());
					Cst.setAge(c.get(Calendar.YEAR) -Integer.parseInt(   customerView.getAge()));
					Cst.setCity(customerView.getCity());
					Cst.setPurchaseCounter();
					Cst.setPriority();
					

					try {
						MessageHandle msg = new MessageHandle(Cst, MessageHandle.ADD_CUSTOMER, false);
						client.sendToServer(msg);
					
					} catch (Exception e) {
						e.printStackTrace( );
					}
					workerMainController.hideWindow();
					customerView.clearFields();
				}
			}
			
			
			else if (event.getSource() == customerView.getBackBtn())
			{
				workerMainController.showWindow();
				customerView.hideWindow();
				customerView.clearFields();
			}
		}
		
		
	}
	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void updataView(String msg){
		
		if (msg.compareTo("new")==0)
		{
			SystemController.displayMessage("New customer successfully added");
			customersList.add(Cst);
		}
		else SystemController.displayErrorMessage("The Customer already exists");

	}
	
	/**
	 * function that adds cities to the city list to show in combobox
	 * @param cities
	 */
	public static void addCities(String [] cities){
		customerView.setCitys(cities);
	}
}



